//
//  MaintenceSettingsCell.m
//  RuiHeApp
//
//  Created by Jun on 2022/11/11.
//

#import "MaintenceSettingsCell.h"
#import "TextInputBoxView.h"
#import "DevSettingModel.h"
#import "ZYSwitch.h"

NSString *const SettingsInputTxtCell = @"SettingsInputTxtCell";
NSString *const SettingsSwitchCell = @"SettingsSwitchCell";
NSString *const SettingsAddOrSubCell = @"SettingsAddOrSubCell";
NSString *const SettingsMoreItemCell = @"SettingsMoreItemCell";
NSString *const SettingsCommandCell = @"SettingsCommandCell";
NSString *const SettingsDateCell = @"SettingsDateCell";
NSString *const SettingsTextCell = @"SettingsTextCell";


@interface MaintenceSettingsCell()<QMUITextFieldDelegate>

@property (nonatomic ,strong) TextInputBoxView *inputView;

@property (nonatomic ,strong) QMUIButton *addBtn;

@property (nonatomic ,strong) QMUIButton *subBtn;

@property (nonatomic ,strong) QMUITextField *textFieldV;

@property (nonatomic ,strong) QMUILabel *titleLab;

@property (nonatomic ,strong) QMUILabel *desLab;

@property (nonatomic ,strong) QMUILabel *unitLab;

@property (nonatomic ,strong) ZYSwitch *switchBtn;

@property (nonatomic ,strong) QMUIButton *date_btn;

@end

@implementation MaintenceSettingsCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI{
    //选择下拉菜单
    if ([self.reuseIdentifier isEqualToString:SettingsInputTxtCell]) {
        _inputView = [[TextInputBoxView alloc] init];
        _inputView.typeBtn.userInteractionEnabled = NO;
        [self.contentView addSubview:_inputView];
        
        [_inputView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(30);
            make.right.mas_offset(-30);
            make.top.mas_offset(5);
            make.bottom.mas_offset(-5);
            make.height.mas_equalTo(45);
        }];
    }
    //开关
    if ([self.reuseIdentifier isEqualToString:SettingsSwitchCell]) {
        _titleLab = [[QMUILabel alloc] init];
        [_titleLab setTextAlignment:NSTextAlignmentLeft];
        [_titleLab setFont:[UIFont systemFontOfSize:11]];
        [_titleLab setTextColor:FontColor(@"#242424")];
        _titleLab.numberOfLines = 2;
        _titleLab.adjustsFontSizeToFitWidth = YES;
        _titleLab.minimumScaleFactor = 0.6;
        [self.contentView addSubview:_titleLab];
        
        _switchBtn = [[ZYSwitch alloc]initWithFrame:CGRectMake(0, 0, 40, 20)];
        _switchBtn.onTintColor = CMainColorGreen;
        _switchBtn.on = NO;
        [self.contentView addSubview:_switchBtn];
        
        [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(30);
            make.top.bottom.mas_offset(0);
            make.height.mas_equalTo(45);
            make.right.equalTo(_switchBtn.mas_left).offset(-5);
        }];
        
        [_switchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_offset(-30);
            make.size.mas_equalTo(CGSizeMake(45, 20));
            make.centerY.mas_offset(0);
        }];
        
        kWeakSelf;
        [[_switchBtn rac_signalForControlEvents:UIControlEventValueChanged] subscribeNext:^(__kindof UIControl * _Nullable x) {
            [weakSelf switchAction];
        }];

    }
    
    if ([self.reuseIdentifier isEqualToString:SettingsAddOrSubCell]) {
        
        _titleLab = [[QMUILabel alloc] init];
        [_titleLab setTextAlignment:NSTextAlignmentLeft];
        [_titleLab setFont:[UIFont systemFontOfSize:11]];
        [_titleLab setTextColor:FontColor(@"#242424")];
        [_titleLab setBackgroundColor:UIColor.whiteColor];
        _titleLab.numberOfLines = 2;
        _titleLab.adjustsFontSizeToFitWidth = YES;
        _titleLab.minimumScaleFactor = 0.6;
        [self.contentView addSubview:_titleLab];
        
        _addBtn = [QMUIButton buttonWithType:UIButtonTypeCustom];
        [_addBtn setBackgroundColor:FontColor(@"#E0E0E0")];
        [_addBtn.layer setCornerRadius:2];
        [_addBtn setImage:[UIImage imageNamed:@"btn_add_icon"] forState:UIControlStateNormal];
        [self.contentView addSubview:_addBtn];
        
        UIView *bgV = [[UIView alloc] init];
        [bgV setBackgroundColor:UIColor.whiteColor];
        [bgV.layer setCornerRadius:0.2];
        [self.contentView addSubview:bgV];
        
        _textFieldV = [[QMUITextField alloc] init];
        [_textFieldV setFont:[UIFont systemFontOfSize:10]];
        [_textFieldV setTextColor:FontColor(@"#242424")];
        [_textFieldV setTextAlignment:NSTextAlignmentCenter];
        [_textFieldV setKeyboardType:UIKeyboardTypeDecimalPad];
        [_textFieldV setDelegate:self];
        [bgV addSubview:_textFieldV];

        
        _subBtn = [QMUIButton buttonWithType:UIButtonTypeCustom];
        [_subBtn setBackgroundColor:FontColor(@"#E0E0E0")];
        [_subBtn.layer setCornerRadius:2];
        [_subBtn setImage:[UIImage imageNamed:@"btn_sub_icon"] forState:UIControlStateNormal];
        [self.contentView addSubview:_subBtn];
        

        QMUILabel *lab = [[QMUILabel alloc] init];
        [lab setTextAlignment:NSTextAlignmentCenter];
        [lab setFont:[UIFont systemFontOfSize:11]];
        [lab setTextColor:UIColor.blackColor];
        [lab setText:@""];
        [self.contentView addSubview:lab];
        self.unitLab = lab;

        [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(30);
            make.top.bottom.mas_offset(0);
            make.height.mas_equalTo(40);
            make.right.equalTo(_addBtn.mas_left).offset(-5);
        }];
        
        [_addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(20, 20));
            make.centerY.mas_offset(0);
            make.right.equalTo(bgV.mas_left).offset(-8);
        }];
        
        [bgV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(40, 20));
            make.centerY.mas_offset(0);
            make.right.equalTo(_subBtn.mas_left).offset(-8);
        }];
        
        [_textFieldV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.insets(UIEdgeInsetsMake(2, 2, 2, 2));
        }];
        
        [_subBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(20, 20));
            make.centerY.mas_offset(0);
            make.right.equalTo(lab.mas_left).offset(-8);
        }];
        
        [lab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(25, 20));
            make.centerY.mas_offset(0);
            make.right.mas_offset(-30);
        }];

        [_textFieldV setText:@"0.0"];
        
        kWeakSelf;
        [[_addBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            [weakSelf addAction];
        }];
        
        [[_subBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            [weakSelf subAction];
        }];
    }
    
    if([self.reuseIdentifier isEqualToString:SettingsMoreItemCell]){
        _titleLab = [[QMUILabel alloc] init];
        [_titleLab setTextAlignment:NSTextAlignmentLeft];
        [_titleLab setFont:[UIFont systemFontOfSize:11]];
        _titleLab.numberOfLines = 2;
        _titleLab.adjustsFontSizeToFitWidth = YES;
        _titleLab.minimumScaleFactor = 0.6;
        [_titleLab setTextColor:FontColor(@"#242424")];
        [self.contentView addSubview:_titleLab];
        
        QMUIButton *arrowBtn = [QMUIButton buttonWithType:UIButtonTypeCustom];
        [arrowBtn setImage:[UIImage imageNamed:@"arrow_right_big"] forState:UIControlStateNormal];
        [arrowBtn setImagePosition:QMUIButtonImagePositionRight];
        [self.contentView addSubview:arrowBtn];
        
        
        [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(30);
            make.top.bottom.mas_offset(0);
            make.height.mas_equalTo(40);
            make.right.equalTo(arrowBtn.mas_left).offset(-5);
        }];
        
        [arrowBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(11, 21));
            make.centerY.mas_offset(0);
            make.right.mas_offset(-15);
        }];
    }
    
    
    if([self.reuseIdentifier isEqualToString:SettingsCommandCell]){//指令样式
        _titleLab = [[QMUILabel alloc] init];
        [_titleLab setTextAlignment:NSTextAlignmentLeft];
        [_titleLab setFont:[UIFont systemFontOfSize:14]];
        _titleLab.numberOfLines = 2;
        _titleLab.adjustsFontSizeToFitWidth = YES;
        _titleLab.minimumScaleFactor = 0.6;
        [_titleLab setTextColor:FontColor(@"#242424")];
        [self.contentView addSubview:_titleLab];
        
        _desLab = [[QMUILabel alloc] init];
        [_desLab setTextAlignment:NSTextAlignmentLeft];
        [_desLab setFont:[UIFont systemFontOfSize:11]];
        [_desLab setTextColor:FontColor(@"#666666")];
        [self.contentView addSubview:_desLab];
        
        QMUIButton *send_btn = [QMUIButton buttonWithType:UIButtonTypeCustom];
        [send_btn setBackgroundColor:FontColor(@"#E0E0E0")];
        [send_btn setTitleColor:FontColor(@"#333333") forState:UIControlStateNormal];
        [send_btn.titleLabel setFont:[UIFont systemFontOfSize:14]];
        [send_btn.layer setCornerRadius:20];
        [send_btn setTitle:@"Send Command" forState:UIControlStateNormal];
        [self.contentView addSubview:send_btn];
        
        kWeakSelf;
        [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(30);
            make.top.mas_offset(5);
            make.height.mas_equalTo(45);
            make.right.equalTo(send_btn.mas_left).offset(-5);
        }];
        
        [_desLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(30);
            make.top.equalTo(weakSelf.titleLab.mas_bottom);
            make.bottom.mas_offset(-5);
            make.height.mas_equalTo(0);
        }];
        
        [send_btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_offset(-30);
            make.size.mas_equalTo(CGSizeMake(130, 40));
            make.centerY.mas_offset(0);
        }];
        
    
        [[send_btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            [weakSelf commandAction];
        }];
    }
    
    if([self.reuseIdentifier isEqualToString:SettingsDateCell]){//日期
        _titleLab = [[QMUILabel alloc] init];
        [_titleLab setTextAlignment:NSTextAlignmentLeft];
        [_titleLab setFont:[UIFont systemFontOfSize:11]];
        [_titleLab setTextColor:FontColor(@"#242424")];
        [_titleLab setBackgroundColor:UIColor.whiteColor];
        _titleLab.numberOfLines = 2;
        _titleLab.adjustsFontSizeToFitWidth = YES;
        _titleLab.minimumScaleFactor = 0.6;
        [self.contentView addSubview:_titleLab];
        
        _desLab = [[QMUILabel alloc] init];
        [_desLab setTextAlignment:NSTextAlignmentLeft];
        [_desLab setFont:[UIFont systemFontOfSize:11]];
        [_desLab setTextColor:FontColor(@"#666666")];
        [self.contentView addSubview:_desLab];
        
        UIImageView *iconV = [[UIImageView alloc] init];
        [iconV setImage:[UIImage imageNamed:@"clock_icon"]];
        [self.contentView addSubview:iconV];
        
        _date_btn = [QMUIButton buttonWithType:UIButtonTypeCustom];
        [_date_btn setBackgroundColor:[UIColor whiteColor]];
        [_date_btn.layer setBorderWidth:0.5];
        [_date_btn.layer setCornerRadius:5];
        [_date_btn.layer setBorderColor:FontColor(@"#999999").CGColor];
        [_date_btn setTitleColor:FontColor(@"#999999") forState:UIControlStateNormal];
        [_date_btn.titleLabel setFont:[UIFont systemFontOfSize:14]];
        [_date_btn.layer setCornerRadius:5];
        [_date_btn setTitle:@"select time" forState:UIControlStateNormal];
        [self.contentView addSubview:_date_btn];
        
        kWeakSelf;
        [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(30);
            make.top.mas_offset(5);
            make.height.mas_equalTo(40);
            make.right.equalTo(weakSelf.date_btn.mas_left).offset(-5);
        }];
        
        [_desLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(30);
            make.top.equalTo(weakSelf.titleLab.mas_bottom);
            make.bottom.mas_offset(-5);
            make.height.mas_equalTo(0);
        }];
        
        [iconV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(22, 22));
            make.centerY.equalTo(weakSelf.date_btn);
            make.right.equalTo(weakSelf.date_btn.mas_left).offset(-5);
        }];
        
        [_date_btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_offset(-35);
            make.size.mas_equalTo(CGSizeMake(110, 35));
            make.centerY.mas_offset(0);
        }];
        
    
        [[_date_btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            [weakSelf dateAction];
        }];
    }
    
    //密码输入
    if([self.reuseIdentifier isEqualToString:SettingsTextCell]){
        _titleLab = [[QMUILabel alloc] init];
        [_titleLab setTextAlignment:NSTextAlignmentLeft];
        [_titleLab setFont:[UIFont systemFontOfSize:11]];
        [_titleLab setTextColor:FontColor(@"#242424")];
        [_titleLab setBackgroundColor:UIColor.whiteColor];
        _titleLab.numberOfLines = 2;
        _titleLab.adjustsFontSizeToFitWidth = YES;
        _titleLab.minimumScaleFactor = 0.6;
        [self.contentView addSubview:_titleLab];
        
        _addBtn = [QMUIButton buttonWithType:UIButtonTypeCustom];
        [_addBtn setBackgroundColor:FontColor(@"#E0E0E0")];
        [_addBtn.layer setCornerRadius:2];
        [_addBtn setImage:[UIImage imageNamed:@"btn_add_icon"] forState:UIControlStateNormal];
        [self.contentView addSubview:_addBtn];
        
        UIView *bgV = [[UIView alloc] init];
        [bgV setBackgroundColor:UIColor.whiteColor];
        [bgV.layer setCornerRadius:5];
        bgV.layer.borderColor = FontColor(@"#999999").CGColor;
        bgV.layer.borderWidth = 0.5;
        [self.contentView addSubview:bgV];
        
        _textFieldV = [[QMUITextField alloc] init];
        [_textFieldV setFont:[UIFont systemFontOfSize:14]];
        [_textFieldV setTextColor:FontColor(@"#242424")];
        [_textFieldV setTextAlignment:NSTextAlignmentLeft];
        [_textFieldV setKeyboardType:UIKeyboardTypeDecimalPad];
        _textFieldV.maximumTextLength = 4;
        [_textFieldV setDelegate:self];
        [bgV addSubview:_textFieldV];
        
        [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(30);
            make.top.bottom.mas_offset(0);
            make.height.mas_equalTo(40);
            make.right.equalTo(bgV.mas_left).offset(-5);
        }];
        
       
        
        [bgV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(90, 35));
            make.centerY.mas_offset(0);
            make.right.mas_offset(-30);
        }];
        
        [_textFieldV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.insets(UIEdgeInsetsMake(2, 10, 2, 10));
        }];
        
        kWeakSelf;
        [[RACSignal merge:@[self.textFieldV.rac_textSignal, RACObserve(self.textFieldV, text)]] subscribeNext:^(NSString* text){
              weakSelf.settingModel.content = text;
        }];
    }
    
}


- (void)commandAction{
    if(self.commandBlock){
        self.commandBlock(self.settingModel);
    }
}


- (void)dateAction{
    if(self.dateBlock){
        self.dateBlock(self.settingModel);
    }
}


- (void)switchAction{
    if ([self.settingModel.content isEqualToString:@"1"]) {
        self.settingModel.content = @"0";
    }else{
        self.settingModel.content = @"1";
    }
    
    if(self.switchBlock){
        self.switchBlock();
    }
    
}

- (void)addAction{
    
    if ([_settingModel.format isEqualToString:@"int"]) {
        //没有获取到阈值,点击加号从小值开始
        NSInteger content = [_settingModel.content integerValue];
        if(_settingModel.content.length == 0){
            content = [_settingModel.min integerValue];
        }
        content = content + [_settingModel.unitLen integerValue];
        if (content>[_settingModel.max integerValue]) {
            return;
        }
        if(_settingModel.content.length == 0){
            _settingModel.content = _settingModel.min;
        }else{
            _settingModel.content = [NSString stringWithFormat:@"%ld",content];
        }
    }
    
    if ([_settingModel.format isEqualToString:@"float"]) {
        //精度问题---
        //没有获取到阈值,点击加号从小值开始
        double content = [_settingModel.content doubleValue];
        if(_settingModel.content.length == 0){
            content = [_settingModel.min doubleValue];
        }
    
        NSDecimalNumber *A = [NSDecimalNumber decimalNumberWithDecimal:@(content).decimalValue];
        NSDecimalNumber *B = [NSDecimalNumber decimalNumberWithDecimal:@([_settingModel.unitLen doubleValue]).decimalValue];
        NSDecimalNumber *resultNum = [A decimalNumberByAdding:B];
        
        if ([resultNum doubleValue] > [_settingModel.max doubleValue]) {
            return;
        }
        //_settingModel.content = [NSString stringWithFormat:@"%.1f",content];
        NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
        formatter.numberStyle = NSNumberFormatterDecimalStyle;
        formatter.minimumFractionDigits = 0;
        formatter.maximumFractionDigits = 2;
        if([_settingModel.ratio isEqualToString:@"0.001"]){
            formatter.maximumFractionDigits = 3;
        }
        [formatter setRoundingMode: NSNumberFormatterRoundDown];
        if(_settingModel.content.length == 0){
            _settingModel.content = _settingModel.min;
        }else{
            _settingModel.content = [[formatter stringFromNumber:resultNum] stringByReplacingOccurrencesOfString:@"," withString:@""];
        }
    }

    [self setSettingModel:self.settingModel];
}

- (void)subAction{
    if ([_settingModel.format isEqualToString:@"int"]) {
        NSInteger content = [_settingModel.content integerValue];
        content = content-[_settingModel.unitLen integerValue];
        if (content<[_settingModel.min integerValue]) {
            return;
        }
        _settingModel.content = [NSString stringWithFormat:@"%ld",content];
    }
    if ([_settingModel.format isEqualToString:@"float"]) {
        double content = [_settingModel.content doubleValue];
//        if (content<[_settingModel.min doubleValue]) {
//            return;
//        }
        NSDecimalNumber *A = [NSDecimalNumber decimalNumberWithDecimal:@(content).decimalValue];
        NSDecimalNumber *B = [NSDecimalNumber decimalNumberWithDecimal:@([_settingModel.unitLen doubleValue]).decimalValue];
        NSDecimalNumber *resultNum = [A decimalNumberBySubtracting:B];
        if ([resultNum doubleValue]<[_settingModel.min doubleValue]) {
            return;
        }
        
        NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
        formatter.numberStyle = NSNumberFormatterDecimalStyle;
        [formatter setRoundingMode: NSNumberFormatterRoundDown];
        _settingModel.content = [[formatter stringFromNumber:resultNum] stringByReplacingOccurrencesOfString:@"," withString:@""];

        /*content = content - [_settingModel.unitLen doubleValue];
        NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
        formatter.numberStyle = NSNumberFormatterDecimalStyle;
        [formatter setRoundingMode: NSNumberFormatterRoundDown];
        _settingModel.content = [[formatter stringFromNumber:@(content)] stringByReplacingOccurrencesOfString:@"," withString:@""];*/
        //_settingModel.content = [NSString stringWithFormat:@"%.1f",content];
    }
    
    [self setSettingModel:self.settingModel];
}

-(void)setDisableMaster:(BOOL)disableMaster
{
    _disableMaster = disableMaster;
    self.addBtn.enabled = !_disableMaster;
    self.subBtn.enabled = !_disableMaster;
    self.textFieldV.userInteractionEnabled = !_disableMaster;
    self.switchBtn.enabled = !_disableMaster;
    self.date_btn.enabled = !_disableMaster;
}

- (void)setSettingModel:(DevSettingModel *)settingModel{
    _settingModel = settingModel;
    if ([self.reuseIdentifier isEqualToString:SettingsInputTxtCell]) {
        [_inputView setModel:_settingModel.inputModel];
        //日月元 工作模式 禁用选项
        [_inputView setDisable:settingModel.disable];
        
        if(self.disableMaster){
            [_inputView setDisable:YES];
        }
        if([settingModel.field isEqualToString:@"Inv1_MachineModel"]){
            
        }
       
    }
    
    if([self.reuseIdentifier isEqualToString:SettingsMoreItemCell]){
        [self.titleLab setText:[NSString stringWithFormat:@"%@\n%@",_settingModel.nickname,_settingModel.comment]];
        //[self.titleLab setText:_settingModel.nickname];
    }
    
    if([self.reuseIdentifier isEqualToString:SettingsCommandCell]){//指令样式
        [self.titleLab setText:_settingModel.nickname.rh_localized];
        if(StrValid(_settingModel.content)){
            [self.desLab setText:_settingModel.content];
            [self.titleLab mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(30);
            }];
            
            [self.desLab mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(15);
            }];
        }else{
            [self.titleLab mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(45);
            }];
            [self.desLab mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(0);
            }];
        }
        
    }

    if ([self.reuseIdentifier isEqualToString:SettingsSwitchCell]) {
        [self.titleLab setText:_settingModel.nickname.rh_localized];
        if (!ValidStr(_settingModel.content)) {
            _settingModel.content = @"0";
        }
        _switchBtn.on = [self.settingModel.content integerValue];
        
        //日月元 工作模式 禁用选项
        _switchBtn.enabled = !settingModel.disable;
        if(self.disableMaster){
            _switchBtn.enabled = NO;
        }
    }
    
    if ([self.reuseIdentifier isEqualToString:SettingsAddOrSubCell]){
        [self.titleLab setText:_settingModel.nickname.rh_localized];
        //[self.titleLab setText:[[_settingModel.nickname stringByAppendingString:@"\n"] stringByAppendingString:_settingModel.field]];
        if (!ValidStr(_settingModel.content)) {
            if ([_settingModel.format isEqualToString:@"int"]) {
//                _settingModel.content = _settingModel.min;
            }
            if ([_settingModel.format isEqualToString:@"float"]) {
//                _settingModel.content = _settingModel.min;
            }
            [_subBtn setEnabled:NO];
            [_addBtn setEnabled:YES];
        }else{
            if ([_settingModel.format isEqualToString:@"int"]) {
                NSInteger content = [_settingModel.content integerValue];
                [_addBtn setEnabled:content>=[_settingModel.max integerValue]?NO:YES];
                [_subBtn setEnabled:content<=[_settingModel.min integerValue]?NO:YES];
            }
            if ([_settingModel.format isEqualToString:@"float"]) {
                NSDecimalNumber *max = [NSDecimalNumber decimalNumberWithString:_settingModel.max];
                NSDecimalNumber *content = [NSDecimalNumber decimalNumberWithString:_settingModel.content];
                NSDecimalNumber *min = [NSDecimalNumber decimalNumberWithString:_settingModel.min];
                NSDecimalNumber *resultNum1 = [max decimalNumberBySubtracting:content];
                NSDecimalNumber *resultNum2 = [content decimalNumberBySubtracting:min];

                [_addBtn setEnabled:resultNum1.doubleValue<_settingModel.unitLen.doubleValue?NO:YES];
                [_subBtn setEnabled:resultNum2.doubleValue<_settingModel.unitLen.doubleValue?NO:YES];
            }
        }
        
        [_textFieldV setText:ValidStr(_settingModel.content)?_settingModel.content:@""];
        [self.unitLab setText:ValidStr(_settingModel.unit)?_settingModel.unit:@""];
        
        if (_settingModel.unchange) {
            [_subBtn setEnabled:NO];
            [_addBtn setEnabled:NO];
            [_textFieldV setEnabled:NO];
        }else{
            [_textFieldV setEnabled:YES];
        }
        
        //HE_BAT或者HE_ALLINONE设备,且是终端用户下master模块不让编辑
        if(self.disableMaster){
            self.subBtn.enabled = NO;
            self.addBtn.enabled = NO;
            self.textFieldV.userInteractionEnabled = NO;
        }
    }
    
    if([self.reuseIdentifier isEqualToString:SettingsDateCell]){//日期
        [self.titleLab setText:_settingModel.nickname.rh_localized];
        
        [_date_btn setTitleColor:StrValid(_settingModel.content)?FontColor(@"#333333"):FontColor(@"#999999") forState:UIControlStateNormal];
        [_date_btn setTitle:_settingModel.content forState:UIControlStateNormal];
        
        [_date_btn sizeToFit];
        
        [_date_btn mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.offset(_date_btn.width<105?105:_date_btn.width+10);
        }];

    }
    //密码
    if ([self.reuseIdentifier isEqualToString:SettingsTextCell]) {
        [self.titleLab setText:_settingModel.nickname.rh_localized];
        self.textFieldV.text = _settingModel.content;
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

#pragma mark - UITextFieldDelegate

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    if ([_settingModel.format isEqualToString:@"int"]) {
        [textField setKeyboardType:UIKeyboardTypeNumberPad];
    }
    if ([_settingModel.format isEqualToString:@"float"]) {
        [textField setKeyboardType:UIKeyboardTypeDecimalPad];
    }
    return YES;
}


- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    //密码
    if([_settingModel.type isEqualToString:@"text"]){
        return YES;
    }
    
    if ([_settingModel.format isEqualToString:@"int"]) {
        NSInteger content = [textField.text integerValue];
        if (content>[_settingModel.max integerValue]) {
            textField.text = _settingModel.max;
        }else if (content<[_settingModel.min integerValue]) {
            textField.text = _settingModel.min;
        }else{
            textField.text = [NSString stringWithFormat:@"%ld",content];
        }
    }
    
    if ([_settingModel.format isEqualToString:@"float"]) {
        CGFloat content = [textField.text doubleValue];
        if (content>[_settingModel.max doubleValue]) {
            textField.text = _settingModel.max;
        }else if (content<[_settingModel.min doubleValue]) {
            textField.text = _settingModel.min;
        }else{
            NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
            formatter.numberStyle = NSNumberFormatterDecimalStyle;
            [formatter setRoundingMode: NSNumberFormatterRoundDown];
            textField.text = [[formatter stringFromNumber:@(content)] stringByReplacingOccurrencesOfString:@"," withString:@""];
        }
    }
    
    [_settingModel setContent:textField.text];
    [self setSettingModel:_settingModel];
    return YES;
}


@end
